package view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;

import model.BoardModel;
import model.Figure;
import control.Kamisado;

public class FieldView extends JComponent {

	private static final long serialVersionUID = 1L;
	private Color clickCol;
	private boolean marked;
	private BoardModel model;
	private Color posCol;
	private int posX;
	private int posY;
	private int sizeX;
	private int sizeY;
	private BoardView view;

	public FieldView(int x, int y, BoardModel m, Kamisado k, BoardView view) {
		super();
		this.view = view;
		marked = false;
		model = m;
		posX = x;
		posY = y;
		posCol = Kamisado.getColor(x, y);
		clickCol = posCol;
		sizeX = getWidth();
		sizeY = getHeight();
		makeListener();
		setPreferredSize(new Dimension(70, 70));
	}

	private void debugRatio() {
		Container f = getParent().getParent().getParent().getParent();
		double fr = (double) f.getWidth() / f.getHeight();
		double tr = (double) sizeX / sizeY;
		if (tr == 1)
			System.out.println(fr);
	}

	private void debugSize() {
		System.out.println("\n 0 = " + this);
		System.out.println("1 = " + getParent());
		System.out.println("2 = " + getParent().getParent());
		System.out.println("3 = " + getParent().getParent().getParent());
		System.out.println("4 = "
				+ getParent().getParent().getParent().getParent());
		Container x = getParent().getParent().getParent().getParent()
				.getParent();
		System.out.println("5 = " + x);
		System.out.println((double) x.getWidth() / x.getHeight());
		System.out.println("6 = "
				+ getParent().getParent().getParent().getParent().getParent()
						.getParent());
	}

	private void drawFigure(Graphics2D g) {
		Figure fig = model.getFigure(posX, posY);
		if (fig != null) {
			Color col = fig.getColor();
			g.setColor(col);
			g.fillArc(sizeX / 4, sizeY / 4, sizeX / 2, sizeY / 2, 0, 360);
			if (fig.getPlayer() != null)
				g.setColor(fig.getPlayer().getColor());
			else
				g.setColor(Color.GRAY);
			g.setStroke(new BasicStroke(sizeX / 10));
			g.drawArc(sizeX / 4, sizeY / 4, sizeX / 2, sizeY / 2, 0, 360);
		}
	}

	private void drawFigure2(Graphics2D g) {

		Figure fig = model.getFigure(posX, posY);

		if (fig != null) {

			Color col;

			if (fig.getPlayer() != null)
				col = fig.getPlayer().getColor();
			else
				col = Color.GRAY;

			g.setColor(col);

			int arcSize = 70;

			int x1 = (100 - arcSize) * sizeX / 2 / 100;
			int y1 = (100 - arcSize) * sizeY / 2 / 100;
			int w = arcSize * sizeX / 100;
			int h = arcSize * sizeY / 100;

			g.fillArc(x1, y1, w, h, 0, 360);

			g.setColor(fig.getColor());
			g.setStroke(new BasicStroke(sizeX / 10));

			int crossSize = 20;

			x1 = (100 - crossSize) * sizeX / 2 / 100;
			y1 = (100 - crossSize) * sizeY / 2 / 100;
			int x2 = ((100 - crossSize) / 2 + crossSize) * sizeX / 100;
			int y2 = ((100 - crossSize) / 2 + crossSize) * sizeY / 100;

			g.drawLine(x1, y1, x2, y2);

			g.drawLine(x1, y2, x2, y1);

		}
	}

	private void drawMark(Graphics2D g) {
		if (marked) {
			g.setColor(Color.GRAY);
			g.drawArc(sizeX / 8, sizeY / 8, 6 * sizeX / 8, 6 * sizeY / 8, 0,
					360);
		}
	}

	private void makeListener() {
		addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
				clickCol = Color.WHITE;
				repaint();
				setClickPos();
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				clickCol = posCol;
				repaint();
			}
		});
	}

	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;

		sizeX = getWidth();
		sizeY = getHeight();

		g2.setColor(clickCol);
		g2.fillRect(0, 0, sizeX, sizeY);

		drawFigure2(g2);
		drawMark(g2);

		// debugRatio();

		// debugSize();

	}

	private void setClickPos() {
		view.setClickPos(posX, posY);
	}

	public void setMarked(boolean b) {
		marked = b;
	}
}
